﻿/*********************************************
 * Logger                                    *
 * Made by:                                  *
 * ------------------------------------------*
 * Papn Kukn (papnkukn@users.sourceforge.net)*
 * Niklas Plüm (l4nm4n@users.sourceforge.net)*
 *********************************************/

using System;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms;
using System.Threading;

namespace VisualPureBasicIDE.CoreGUI
{
    public static class Logger
    {
        private static Exception _exception;
        private static Mutex _sendmutex;

        /// <summary>
        /// Logs an exception
        /// </summary>
        /// <param name="message">The exception message</param>
        public static void LogException(string message)
        {
            // That should be enough for now
            Console.WriteLine(String.Format("An exception occurred: {0}", message));
        }

        /// <summary>
        /// Logs an exception
        /// </summary>
        /// <param name="exception">The exception</param>
        public static void LogException(Exception exception)
        {
            // That should be enough for now
            Console.WriteLine(String.Format("An exception occurred: {0}\nStack Trace:  {1}", exception.Message, exception.StackTrace));
        }

        /// <summary>
        /// Sends an exception to e-mail (could be received and discussed by all developers)
        /// </summary>
        private static void SendExceptionSync()
        {
            // That should be enough for now but later exceptions could also be logged in bug tracking system
            if (_exception != null)
            {
                try
                {
                    _sendmutex.WaitOne();
                    // Create e-mail content
                    var subject = "Visual PureBasic IDE -> Exception from " + Dns.GetHostName();
                    var message =
                        @"<html>
                      <body>
                        Time: {4}<br />
                        Message: <strong>{0}</strong><br />
                        Source: {1}<br />
                        Target: {2}<br />
                        StackTrace: {3}
                      </body>
                      </html>";
                    message = string.Format(message, _exception.Message, _exception.Source, _exception.TargetSite, _exception.StackTrace.Replace("\n", "<br />"), DateTime.Now);

                    // Send e-mail
                    var client = new SmtpClient(InvariantStrings.SmtpServer);
                    var address = new MailAddress(InvariantStrings.ErrorEmail);
                    var mail = new MailMessage(address, address) {Subject = subject, Body = message, IsBodyHtml = true};

                    client.Port = 587;
                    client.Credentials = new NetworkCredential(InvariantStrings.LoginName, InvariantStrings.Password);
                    client.EnableSsl = true;
                    client.Send(mail);
                    _sendmutex.ReleaseMutex();
                }
                catch
                {
                    // Do not process exception, it may loop and cause stack overflow
                }
            }
        }

        public static void SendException(Exception ex)
        {
            if (_sendmutex == null) _sendmutex = new Mutex();
            _exception = ex;
            var mi = new MethodInvoker(SendExceptionSync);
            mi.BeginInvoke(ExceptionSent, null);
        }

        private static void ExceptionSent(IAsyncResult result)
        { 
            // Stub 
        }

        /// <summary>
        /// Logs a debug message
        /// </summary>
        /// <param name="message">The debug message to log</param>
        public static void LogDebugMessage(string message)
        {
            // That should be enough for now
            Console.WriteLine(String.Format("DEBUG: {0}", message));
        }

        /// <summary>
        /// Logs a debug object (calls the "ToString" method)
        /// </summary>
        /// <param name="object">The debug object to log</param>
        public static void LogDebugObject(object @object)
        {
            // That should be enough for now
            Console.WriteLine(String.Format("DEBUG: {0}", @object));
        }
    }
}